package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.SysRole;
import com.czkt.pojo.SysRoleRight;
import com.czkt.service.ISysRightService;
import com.czkt.service.ISysRoleRightService;
import com.czkt.service.ISysRoleService;
import com.czkt.service.ISysUserService;
import com.czkt.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    ISysRoleService sysRoleService;

    @RequestMapping("/list")
    public Object list(SysRole sysRole, @RequestParam(defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "2") Integer pageSize, Model model){
        IPage<SysRole> sysRoleIPage = new Page<>(pageIndex,pageSize);

        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();

        queryWrapper.lambda().like(StringUtils.hasText(sysRole.getRoleName()),SysRole::getRoleName,sysRole.getRoleName());

        Page<SysRole> sysRolePage = (Page<SysRole>) sysRoleService.page(sysRoleIPage,queryWrapper);

        PageUtils pageUtils = new PageUtils();

        BeanUtils.copyProperties(sysRolePage,pageUtils);

        model.addAttribute("rolePage",pageUtils);
        model.addAttribute("roleName",sysRole.getRoleName());
        return "role/list";
    }

    @Autowired
    ISysRightService sysRightService;

    @RequestMapping("/add")
    public Object add(Model model){
        model.addAttribute("rights",sysRightService.list());
        return "role/add";
    }

    @Autowired
    ISysRoleRightService sysRoleRightService;

    @RequestMapping("/save")
    public Object save(SysRole sysRole,String[] rightCodes,Model model){
        System.out.println("rightCods:" + rightCodes);

        if (sysRoleService.saveOrUpdate(sysRole)){
            //先将原有数据权限删除
            QueryWrapper<SysRoleRight> queryWrapper = new QueryWrapper();
            queryWrapper.lambda().eq(SysRoleRight::getRfRoleId,sysRole.getRoleId());
            sysRoleRightService.remove(queryWrapper);

            //再添加该有的权限
            List<SysRoleRight> list = new ArrayList<>();
            for (String rightCode : rightCodes){
                SysRoleRight sysRoleRight = new SysRoleRight();
                sysRoleRight.setRfRightCode(rightCode);
                sysRoleRight.setRfRoleId(sysRole.getRoleId());
                list.add(sysRoleRight);
            }
            sysRoleRightService.saveBatch(list);
            return "redirect:/role/list";
        } else if (sysRole.getRoleId() != 0){
            return "role/edit";
        } else {
            return "redirect:role/add";
        }
    }

    @Autowired
    ISysUserService sysUserService;

    @RequestMapping("/edit")
    public Object toEdit(long roleId,Model model){
        model.addAttribute("role",sysRoleService.getById(roleId));
        model.addAttribute("rights",sysRightService.list());
        List<SysRoleRight> rights = sysUserService.getSysRoleById(roleId);
        Set<String> stringSet = rights.stream().map(roleRight -> roleRight.getRfRightCode()).collect(Collectors.toSet());
        model.addAttribute("roleRights",stringSet);
        return "role/edit";
    }

    @RequestMapping("/del{roleId}")
    @ResponseBody
    public Object del(@PathVariable("roleId") long roleId){
        Map<String,String> map = new HashMap<>();
        if (sysRoleService.removeById(roleId)){
            map.put("delResult","true");
        }
        return map;
    }

}